﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Data;
using Billing.Utilities;
using Billing.Dtos;
using Billing.Controls;
using System.Configuration;
using System.Net.NetworkInformation;

namespace Billing.Windows
{
    public partial class Orders : Window
    {
        public Orders()
        {
            try
            {
                InitializeComponent();

                CheckConnectivity();

                for (int cnt = 0; cnt < 2; cnt++)
                {
                    cmbDate.Items.Add(new ComboBoxItem { Tag = DateTime.Now.AddDays(cnt), Content = DateTime.Now.AddDays(cnt).DayOfWeek.ToString() + "-" + DateTime.Now.AddDays(cnt).ToString("dd-MMM-yyyy") });
                }
                cmbDate.SelectedIndex = 1;
                List<OrderItemControl> mainLst = new List<OrderItemControl>();
                foreach (BillingGridItemDto dto in CacheManager.BillingItems)
                {
                    OrderItemControl ctrl = new OrderItemControl();
                    ctrl.txtCode.Text = dto.Code.ToString();
                    ctrl.txtDesc.Text = dto.Name;
                    mainLst.Add(ctrl);
                }
                lstOrderGrid.ItemsSource = mainLst;
                cmbDate.Focus();
            }
            catch (Exception) { }
        }

        private void CheckConnectivity()
        {
            var ping = new Ping();
            var options = new PingOptions { DontFragment = true };

            //just need some data. this sends 10 bytes. 
            var buffer = Encoding.ASCII.GetBytes(new string('z', 10));
            PingReply reply = null;
            try
            {
                reply = ping.Send(ConfigurationManager.AppSettings["MachineToPing"], 60, buffer, options);
            }
            catch (Exception)
            {
                reply = null;
            }

            if (reply != null && reply.Status == IPStatus.Success)
            {
                success.Visibility = Visibility.Visible;
                failure.Visibility = Visibility.Hidden;
            }
            else
            {
                success.Visibility = Visibility.Hidden;
                failure.Visibility = Visibility.Visible;
            }
        }

        private void Window_KeyUp(object sender, KeyEventArgs e)
        {
            try
            {
                if (e.Key == Key.Escape)
                {
                    if (MessageBoxResult.OK == MessageBox.Show("Do you want to close?", "Close", MessageBoxButton.OKCancel, MessageBoxImage.Question))
                        this.Close();
                }
                if (e.Key == Key.Enter && cmbDate.IsFocused)
                {
                    lstOrderGrid.Focus();
                    lstOrderGrid.SelectedIndex = 0;
                    (lstOrderGrid.Items[0] as OrderItemControl).txtTrip1Qty.Focus();
                    //lstOrderGrid.Items[0]
                }
                else if (!cmbDate.IsFocused)
                {
                    if (e.Key == Key.Up)
                    {
                        if (lstOrderGrid.SelectedIndex != 0)
                        {
                            int trip = 1;
                            if ((lstOrderGrid.Items[lstOrderGrid.SelectedIndex] as OrderItemControl).txtTrip1Qty.Background == Brushes.White)
                                trip = 1;
                            else if ((lstOrderGrid.Items[lstOrderGrid.SelectedIndex] as OrderItemControl).txtTrip2Qty.Background == Brushes.White)
                                trip = 2;
                            else if ((lstOrderGrid.Items[lstOrderGrid.SelectedIndex] as OrderItemControl).txtTrip3Qty.Background == Brushes.White)
                                trip = 3;
                            else if ((lstOrderGrid.Items[lstOrderGrid.SelectedIndex] as OrderItemControl).txtTrip4Qty.Background == Brushes.White)
                                trip = 4;

                            lstOrderGrid.SelectedIndex -= 1;
                            if (trip == 1)
                                (lstOrderGrid.Items[lstOrderGrid.SelectedIndex] as OrderItemControl).txtTrip1Qty.Focus();
                            else if (trip == 2)
                                (lstOrderGrid.Items[lstOrderGrid.SelectedIndex] as OrderItemControl).txtTrip2Qty.Focus();
                            else if (trip == 3)
                                (lstOrderGrid.Items[lstOrderGrid.SelectedIndex] as OrderItemControl).txtTrip3Qty.Focus();
                            else if (trip == 4)
                                (lstOrderGrid.Items[lstOrderGrid.SelectedIndex] as OrderItemControl).txtTrip4Qty.Focus();
                        }
                    }
                    else if (e.Key == Key.Down)
                    {
                        if (lstOrderGrid.SelectedIndex + 1 <= lstOrderGrid.Items.Count)
                        {
                            int trip = 1;
                            if ((lstOrderGrid.Items[lstOrderGrid.SelectedIndex] as OrderItemControl).txtTrip1Qty.Background == Brushes.White)
                                trip = 1;
                            else if ((lstOrderGrid.Items[lstOrderGrid.SelectedIndex] as OrderItemControl).txtTrip2Qty.Background == Brushes.White)
                                trip = 2;
                            else if ((lstOrderGrid.Items[lstOrderGrid.SelectedIndex] as OrderItemControl).txtTrip3Qty.Background == Brushes.White)
                                trip = 3;
                            else if ((lstOrderGrid.Items[lstOrderGrid.SelectedIndex] as OrderItemControl).txtTrip4Qty.Background == Brushes.White)
                                trip = 4;

                            lstOrderGrid.SelectedIndex += 1;
                            if (trip == 1)
                                (lstOrderGrid.Items[lstOrderGrid.SelectedIndex] as OrderItemControl).txtTrip1Qty.Focus();
                            else if (trip == 2)
                                (lstOrderGrid.Items[lstOrderGrid.SelectedIndex] as OrderItemControl).txtTrip2Qty.Focus();
                            else if (trip == 3)
                                (lstOrderGrid.Items[lstOrderGrid.SelectedIndex] as OrderItemControl).txtTrip3Qty.Focus();
                            else if (trip == 4)
                                (lstOrderGrid.Items[lstOrderGrid.SelectedIndex] as OrderItemControl).txtTrip4Qty.Focus();
                        }
                    }
                    else if (e.Key == Key.PageDown)
                    {
                        int trip = 1;
                        if ((lstOrderGrid.Items[lstOrderGrid.SelectedIndex] as OrderItemControl).txtTrip1Qty.Background == Brushes.White)
                        {
                            trip = 1;
                            (lstOrderGrid.Items[lstOrderGrid.SelectedIndex] as OrderItemControl).txtTrip1Qty.Background = Brushes.Transparent;
                            (lstOrderGrid.Items[lstOrderGrid.SelectedIndex] as OrderItemControl).txtTrip1Qty.Foreground = Brushes.White;
                        }
                        else if ((lstOrderGrid.Items[lstOrderGrid.SelectedIndex] as OrderItemControl).txtTrip2Qty.Background == Brushes.White)
                        {
                            trip = 2;
                            (lstOrderGrid.Items[lstOrderGrid.SelectedIndex] as OrderItemControl).txtTrip2Qty.Background = Brushes.Transparent;
                            (lstOrderGrid.Items[lstOrderGrid.SelectedIndex] as OrderItemControl).txtTrip2Qty.Foreground = Brushes.White;
                        }
                        else if ((lstOrderGrid.Items[lstOrderGrid.SelectedIndex] as OrderItemControl).txtTrip3Qty.Background == Brushes.White)
                        {
                            trip = 3;
                            (lstOrderGrid.Items[lstOrderGrid.SelectedIndex] as OrderItemControl).txtTrip3Qty.Background = Brushes.Transparent;
                            (lstOrderGrid.Items[lstOrderGrid.SelectedIndex] as OrderItemControl).txtTrip3Qty.Foreground = Brushes.White;
                        }
                        else if ((lstOrderGrid.Items[lstOrderGrid.SelectedIndex] as OrderItemControl).txtTrip4Qty.Background == Brushes.White)
                        {
                            trip = 4;
                            (lstOrderGrid.Items[lstOrderGrid.SelectedIndex] as OrderItemControl).txtTrip4Qty.Background = Brushes.Transparent;
                            (lstOrderGrid.Items[lstOrderGrid.SelectedIndex] as OrderItemControl).txtTrip4Qty.Foreground = Brushes.White;
                        }

                        if (lstOrderGrid.Items.Count > lstOrderGrid.SelectedIndex + 9)
                            lstOrderGrid.SelectedIndex += 10;
                        else
                            lstOrderGrid.SelectedIndex = lstOrderGrid.Items.Count - 1;

                        if (trip == 1)
                        {
                            (lstOrderGrid.Items[lstOrderGrid.SelectedIndex] as OrderItemControl).Focus();
                            (lstOrderGrid.Items[lstOrderGrid.SelectedIndex] as OrderItemControl).txtTrip1Qty.Focus();
                            (lstOrderGrid.Items[lstOrderGrid.SelectedIndex] as OrderItemControl).txtTrip1Qty.SelectAll();
                        }
                        else if (trip == 2)
                        {
                            (lstOrderGrid.Items[lstOrderGrid.SelectedIndex] as OrderItemControl).Focus();
                            (lstOrderGrid.Items[lstOrderGrid.SelectedIndex] as OrderItemControl).txtTrip2Qty.Focus();
                            (lstOrderGrid.Items[lstOrderGrid.SelectedIndex] as OrderItemControl).txtTrip2Qty.SelectAll();
                        }
                        else if (trip == 3)
                        {
                            (lstOrderGrid.Items[lstOrderGrid.SelectedIndex] as OrderItemControl).Focus();
                            (lstOrderGrid.Items[lstOrderGrid.SelectedIndex] as OrderItemControl).txtTrip3Qty.SelectAll();
                            (lstOrderGrid.Items[lstOrderGrid.SelectedIndex] as OrderItemControl).txtTrip3Qty.Focus();
                        }
                        else if (trip == 4)
                        {
                            (lstOrderGrid.Items[lstOrderGrid.SelectedIndex] as OrderItemControl).Focus();
                            (lstOrderGrid.Items[lstOrderGrid.SelectedIndex] as OrderItemControl).txtTrip4Qty.SelectAll();
                            (lstOrderGrid.Items[lstOrderGrid.SelectedIndex] as OrderItemControl).txtTrip4Qty.Focus();
                        }

                        lstOrderGrid.ScrollIntoView(lstOrderGrid.Items[lstOrderGrid.SelectedIndex] as OrderItemControl);
                    }
                    else if (e.Key == Key.PageUp)
                    {
                        int trip = 1;
                        if ((lstOrderGrid.Items[lstOrderGrid.SelectedIndex] as OrderItemControl).txtTrip1Qty.Background == Brushes.White)
                        {
                            trip = 1;
                            (lstOrderGrid.Items[lstOrderGrid.SelectedIndex] as OrderItemControl).txtTrip1Qty.Background = Brushes.Transparent;
                            (lstOrderGrid.Items[lstOrderGrid.SelectedIndex] as OrderItemControl).txtTrip1Qty.Foreground = Brushes.White;
                        }
                        else if ((lstOrderGrid.Items[lstOrderGrid.SelectedIndex] as OrderItemControl).txtTrip2Qty.Background == Brushes.White)
                        {
                            trip = 2;
                            (lstOrderGrid.Items[lstOrderGrid.SelectedIndex] as OrderItemControl).txtTrip2Qty.Background = Brushes.Transparent;
                            (lstOrderGrid.Items[lstOrderGrid.SelectedIndex] as OrderItemControl).txtTrip2Qty.Foreground = Brushes.White;
                        }
                        else if ((lstOrderGrid.Items[lstOrderGrid.SelectedIndex] as OrderItemControl).txtTrip3Qty.Background == Brushes.White)
                        {
                            trip = 3;
                            (lstOrderGrid.Items[lstOrderGrid.SelectedIndex] as OrderItemControl).txtTrip3Qty.Background = Brushes.Transparent;
                            (lstOrderGrid.Items[lstOrderGrid.SelectedIndex] as OrderItemControl).txtTrip3Qty.Foreground = Brushes.White;
                        }
                        else if ((lstOrderGrid.Items[lstOrderGrid.SelectedIndex] as OrderItemControl).txtTrip4Qty.Background == Brushes.White)
                        {
                            trip = 4;
                            (lstOrderGrid.Items[lstOrderGrid.SelectedIndex] as OrderItemControl).txtTrip4Qty.Background = Brushes.Transparent;
                            (lstOrderGrid.Items[lstOrderGrid.SelectedIndex] as OrderItemControl).txtTrip4Qty.Foreground = Brushes.White;
                        }

                        if (lstOrderGrid.SelectedIndex - 9 > 0)
                            lstOrderGrid.SelectedIndex -= 10;
                        else
                            lstOrderGrid.SelectedIndex = 0;

                        if (trip == 1)
                        {
                            (lstOrderGrid.Items[lstOrderGrid.SelectedIndex] as OrderItemControl).Focus();
                            (lstOrderGrid.Items[lstOrderGrid.SelectedIndex] as OrderItemControl).txtTrip1Qty.Focus();
                            (lstOrderGrid.Items[lstOrderGrid.SelectedIndex] as OrderItemControl).txtTrip1Qty.SelectAll();
                        }
                        else if (trip == 2)
                        {
                            (lstOrderGrid.Items[lstOrderGrid.SelectedIndex] as OrderItemControl).Focus();
                            (lstOrderGrid.Items[lstOrderGrid.SelectedIndex] as OrderItemControl).txtTrip2Qty.Focus();
                            (lstOrderGrid.Items[lstOrderGrid.SelectedIndex] as OrderItemControl).txtTrip2Qty.SelectAll();
                        }
                        else if (trip == 3)
                        {
                            (lstOrderGrid.Items[lstOrderGrid.SelectedIndex] as OrderItemControl).Focus();
                            (lstOrderGrid.Items[lstOrderGrid.SelectedIndex] as OrderItemControl).txtTrip3Qty.SelectAll();
                            (lstOrderGrid.Items[lstOrderGrid.SelectedIndex] as OrderItemControl).txtTrip3Qty.Focus();
                        }
                        else if (trip == 4)
                        {
                            (lstOrderGrid.Items[lstOrderGrid.SelectedIndex] as OrderItemControl).Focus();
                            (lstOrderGrid.Items[lstOrderGrid.SelectedIndex] as OrderItemControl).txtTrip4Qty.SelectAll();
                            (lstOrderGrid.Items[lstOrderGrid.SelectedIndex] as OrderItemControl).txtTrip4Qty.Focus();
                        }

                        lstOrderGrid.ScrollIntoView(lstOrderGrid.Items[lstOrderGrid.SelectedIndex] as OrderItemControl);
                    }
                    else if (e.Key == Key.F1)
                    {
                        if (MessageBoxResult.Yes == MessageBox.Show("Do you want to send update to Manufacturing Department?", "Send", MessageBoxButton.YesNo, MessageBoxImage.Question))
                        {
                            bool check = false;
                            //Check whether anything is filled or not
                            foreach (OrderItemControl item in lstOrderGrid.Items)
                            {
                                if (item.txtTrip1Qty.Text != "0" || item.txtTrip2Qty.Text != "0" || item.txtTrip3Qty.Text != "0" || item.txtTrip4Qty.Text != "0")
                                {
                                    check = true;
                                    break;
                                }
                            }

                            if (!(check))
                            {
                                MessageBox.Show("Not valid data. Nothing to Save", "Invalid Data", MessageBoxButton.OK, MessageBoxImage.Warning);
                                return;
                            }

                            //Date, Branch, Storage.LoginUser, Items
                            if (Database.AddOrder(DateTime.Parse((cmbDate.SelectedItem as ComboBoxItem).Tag.ToString()), ConfigurationManager.AppSettings["Branch"].ToString(), Storage.LoginUser, GetItemsXML()) > 0)
                            {
                                MessageBox.Show("Successfully Submitted to Server", "Success", MessageBoxButton.OK, MessageBoxImage.Information);
                                this.Close();
                            }
                            else
                            {
                                MessageBox.Show("Failed to Transfer", "Failure", MessageBoxButton.OK, MessageBoxImage.Error);
                            }
                        }
                    }
                }
            }
            catch (Exception) { }
        }

        private string GetItemsXML()
        {
            try
            {
                StringBuilder strBldr = new StringBuilder();
                strBldr.AppendFormat("<?xml version=\"1.0\"?><Order>");
                foreach (OrderItemControl ctrl in lstOrderGrid.Items)
                {
                    if (ctrl.txtTrip1Qty.Text != "0" || ctrl.txtTrip2Qty.Text != "0" || ctrl.txtTrip3Qty.Text != "0" || ctrl.txtTrip4Qty.Text != "0")
                        strBldr.AppendFormat("<Item ItemID=\"{0}\" Trip1=\"{1}\" Trip2=\"{2}\" Trip3=\"{3}\" Trip4=\"{4}\" />", ctrl.txtCode.Text, ctrl.txtTrip1Qty.Text, ctrl.txtTrip2Qty.Text, ctrl.txtTrip3Qty.Text, ctrl.txtTrip4Qty.Text);
                }
                strBldr.Append("</Order>");
                return strBldr.ToString();
            }
            catch (Exception)
            { }
            return string.Empty;
        }
    }
}
